package masterjava.common.dao.hibernate;

import masterjava.common.shared.entity.GenericEntity;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * User: Grigory Kislin
 * Date: 27/06/11
 */
public class TransactionHibernateDao {

    private HibernateDaoHelper hibernateDaoHelper;

    public void setHibernateDaoHelper(HibernateDaoHelper hibernateDaoHelper) {
        this.hibernateDaoHelper = hibernateDaoHelper;
    }

    public HibernateDaoHelper getHibernateDaoHelper() {
        return hibernateDaoHelper;
    }

    @Transactional(readOnly = true)
    public <T extends GenericEntity> List<T> loadAll(Class<T> persistentClass) {
        return hibernateDaoHelper.loadAll(persistentClass);
    }

    @Transactional(readOnly = true)
    public <T extends GenericEntity, ID extends Serializable> T get(Class<T> persistentClass, ID id) {
        return hibernateDaoHelper.get(persistentClass, id);
    }

    @Transactional(readOnly = false)
    public Serializable saveOrUpdate(GenericEntity entity) {
        return hibernateDaoHelper.saveOrUpdate(entity);
    }

    @Transactional(readOnly = false)
    public void delete(Class<? extends GenericEntity> persistentClass, Serializable id) {
        hibernateDaoHelper.delete(persistentClass, id);
    }

    @Transactional(readOnly = true)
    public <T extends GenericEntity> List<T> findByNamedQueryAndNamedParam(String queryName, String paramName, Serializable value) {
        return hibernateDaoHelper.findByNamedQueryAndNamedParam(queryName, paramName, value);
    }

    @Transactional(readOnly = true)
    public GenericEntity findUnique(String queryName, String paramName, Serializable value) {
        return hibernateDaoHelper.findUnique(queryName, paramName, value);
    }

    @Transactional(readOnly = true)
    public GenericEntity findUnique(String queryName, String[] paramNames, Serializable[] values) {
        return hibernateDaoHelper.findUnique(queryName, paramNames, values);
    }

    @Transactional(readOnly = true)
    public <T extends GenericEntity> List<T> findByNamedQueryAndNamedParam(final String queryName, final String[] paramNames, final Serializable[] values) {
        return hibernateDaoHelper.findByNamedQueryAndNamedParam(queryName, paramNames, values);
    }
}
